package com.dstation.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dstation.domain.*;
import com.dstation.service.interf.IDynamicExamineService;
import com.dstation.utils.DynamicIntelligenceExamine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;

@Controller
@RequestMapping("/dynamicExamine")
public class DynamicExamineController {

    @Autowired
    IDynamicExamineService dynamicExamineService;

    @RequestMapping("/dynamicExaminePage")
    public String dynamicExaminePage(@RequestParam(value = "page")Integer page, ModelMap modelMap) {
        page=page==null?1:page;
        IPage<DynamicInfo> dynamicInfoIPage = dynamicExamineService.findAllNotAccessDynamicInfo(new Page<VideoInfo>(page,10));
        System.out.println(dynamicInfoIPage.getRecords());
        modelMap.put("dynamicInfoIPage", dynamicInfoIPage);
        return "admin/dynamicExamine";
    }

    /**
     * 人工通过/拒绝通过动态审核
     * @param dynamicId
     * @param isPass
     * @return
     */
    @RequestMapping("accessExamine")
    @ResponseBody
    public JSONObject accessExamine(@RequestParam("id")long dynamicId, @RequestParam("isPass")boolean isPass,
                                    HttpSession session){
        //1. 首先更新动态状态信息
        JSONObject jsonObject = new JSONObject();
        boolean modifyRes = dynamicExamineService.alterDynamicStatusByVideoId(dynamicId, isPass);
        if(modifyRes == true) {
            jsonObject.put("res", true);
        } else {
            jsonObject.put("res", false);
        }
        //2.插入一条检测记录
        DynamicReviewLogInfo dynamicReviewLogInfo = new DynamicReviewLogInfo();
        dynamicReviewLogInfo.setDynamicId(dynamicId);
        dynamicReviewLogInfo.setAdminId(((AdministratorInfo)session.getAttribute("adminInfo")).getAdminId());
        dynamicReviewLogInfo.setCreateDate(new Timestamp(System.currentTimeMillis()));
        boolean saveRes = dynamicExamineService.addDynamicReviewLogInfo(dynamicReviewLogInfo);
        if(saveRes == true) {
            jsonObject.put("res", true);
        } else {
            jsonObject.put("res", false);
        }
        return jsonObject;
    }

    /**
     * 动态的智能审核
     * @param dynamicId
     * @return
     */
    @RequestMapping("/intelligenceExamine")
    @ResponseBody
    public JSONObject intelligenceExamine(@RequestParam("dynamicId")long dynamicId) {
        //1. 根据dynamicID查询动态信息
        DynamicInfo dynamicInfo = dynamicExamineService.findDynamicInfoByDynamicID(dynamicId);
        //2.检测动态
        JSONObject jsonObject = null;   //提交结果
        try {
            jsonObject = DynamicIntelligenceExamine.uploadExamine(dynamicInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //3.得到taskID
        //3. 根据提交的结果检测视频
        JSONArray dataArr = jsonObject.getJSONArray("data");
        JSONObject data1 = (JSONObject) dataArr.get(0);
        JSONObject uploadRes = new JSONObject();
        String taskId = (String) data1.get("taskId");
        String dataId = (String) data1.get("dataId");
        uploadRes.put("taskId", taskId);
        uploadRes.put("dataId", dataId);
        return uploadRes;
    }

    /**
     * 查询动态审核结果
     * @return
     */
    @RequestMapping("/referExamineResByTaskId")
    @ResponseBody
    public JSONObject referExamineResByTaskId(@RequestParam("dynamicId")long dynamicId,
                                              @RequestParam("taskId")String taskId,
                                              @RequestParam("dataId")String dataId) {
        //1. 根据dynamicID查询动态信息
        DynamicInfo dynamicInfo = dynamicExamineService.findDynamicInfoByDynamicID(dynamicId);
        JSONObject jsonObject = null;
        try {
            jsonObject = DynamicIntelligenceExamine.referExmaineRes(dynamicInfo, taskId,dataId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //2. 查看是否通过
        JSONObject res = new JSONObject();
        Integer code = jsonObject.getInteger("code");
        res.put("code", code);
        return res;
    }
}
